package io.renren.modules.udp.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import io.renren.common.utils.PublicUtil;
import io.renren.common.utils.RedisUtils;
import io.renren.modules.udp.dao.R5rmSkillstatusDao;
import io.renren.modules.udp.entity.R5rmObjectsEntity;
import io.renren.modules.udp.entity.R5rmSkillstatusEntity;
import io.renren.modules.udp.service.R5rmSkillstatusService;
import io.renren.modules.udp.utils.UdpStringUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Map;

import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import javax.annotation.Resource;


@Service("r5rmSkillstatusService")
public class R5rmSkillstatusServiceImpl extends ServiceImpl<R5rmSkillstatusDao, R5rmSkillstatusEntity> implements R5rmSkillstatusService {

    @Autowired
    private RedisUtils redisUtils;
    @Resource
    private R5rmSkillstatusDao skillstatusDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String key = (String)params.get("key");
        Page<R5rmSkillstatusEntity> page = this.selectPage(
                new Query<R5rmSkillstatusEntity>(params).getPage(),
                new EntityWrapper<R5rmSkillstatusEntity>().like(StringUtils.isNotBlank(key),"itCode", key)
        );

        return new PageUtils(page);
    }

    @Override
    public R5rmSkillstatusEntity getUdp(String str) {

        String[] s2 = str.split("\\s+");
        String wz = s2[24];
        System.out.println(wz);
        R5rmSkillstatusEntity skillstatusEntity = new R5rmSkillstatusEntity();
        //根据文档算好数组起始
        int s = 19;
        //设备号码
        String zh = s2[3] + s2[4] + s2[5] + s2[6] + s2[7] + s2[8] + s2[9] + s2[10] + s2[11] + s2[12] + s2[13];
        skillstatusEntity.setSsObj(UdpStringUtils.hexStrToStr(zh));
        //redis增加数据
        redisUtils.set(skillstatusEntity.getSsObj(),str);
        //光伏板输入电压(V)
        skillstatusEntity.setSsUdfchar01(PublicUtil.result(s2[s+10]+s2[s+11],2,"V"));
        //光伏板输入电流(A)
        skillstatusEntity.setSsUdfchar02(PublicUtil.result(s2[s+12]+s2[s+13],1,"A"));
        //光伏板输入功率(W)
        skillstatusEntity.setSsUdfchar03(PublicUtil.result(s2[s+59]+s2[s+60],0,"W"));
        //光感输入电压(V)    有误
        skillstatusEntity.setSsUdfchar04(PublicUtil.result(s2[s+44]+s2[s+45],2,"V"));
        //LED驱动器输出电压（V)
        skillstatusEntity.setSsUdfchar05(PublicUtil.result(s2[s+20]+s2[s+21],1,"V"));
        //LED驱动器输出电流（A)
        skillstatusEntity.setSsUdfchar06(PublicUtil.result(s2[s+22]+s2[s+23],2,"A"));
        //LED驱动器输出功率（W)
        skillstatusEntity.setSsUdfchar07(PublicUtil.result(s2[s+24]+s2[s+25],1,"W"));
        //LED驱动器输出状态  有误
        skillstatusEntity.setSsUdfchar08("有误");
        //LED驱动器已输出时间（H) ====LED输出总时间
        skillstatusEntity.setSsUdfchar09(Long.parseLong(s2[s+37], 16)+"H");
        //蓄电池电压（V)
        skillstatusEntity.setSsUdfchar10(PublicUtil.result(s2[s+18]+s2[s+19],2,"V"));
        //蓄电池充电电流（A)
        skillstatusEntity.setSsUdfchar11(PublicUtil.result(s2[s+61]+s2[s+62],1,"A"));
        //蓄电池充电功率（W)
        skillstatusEntity.setSsUdfchar12(PublicUtil.result(s2[s+63]+s2[s+64],0,"W"));
        //蓄电池总充电量（度）
        skillstatusEntity.setSsUdfchar13(PublicUtil.result(s2[s+54]+s2[s+55]+s2[s+56],1,"度"));
        //蓄电池剩余电量比
        skillstatusEntity.setSsUdfchar14(Long.parseLong(s2[s+30], 16)+"%");
        //蓄电池当前状态
        long dczt = Long.parseLong(s2[s+31], 16);
        if (dczt==0){
            skillstatusEntity.setSsUdfchar15("正常");
        }else if (dczt==1){
            skillstatusEntity.setSsUdfchar15("过压");
        }else if (dczt==2){
            skillstatusEntity.setSsUdfchar15("欠压");
        }else {
            skillstatusEntity.setSsUdfchar15("获取出错");
        }
        //风机输入直流电压(V)
        skillstatusEntity.setSsUdfchar16(PublicUtil.result(s2[s+14]+s2[s+15],2,"V"));
        //风机输入直流电流(A)
        skillstatusEntity.setSsUdfchar17(PublicUtil.result(s2[s+16]+s2[s+17],1,"A"));
        //风机热效电流(W)  有误
        skillstatusEntity.setSsUdfchar18(PublicUtil.result(s2[s+8]+s2[s+9],2,"A"));
        //风机输入直流功率电压(V)  有误
        skillstatusEntity.setSsUdfchar19(PublicUtil.result(s2[s+10]+s2[s+11],2,"V"));
        //风机有限充电电流（A)
        skillstatusEntity.setSsUdfchar20(PublicUtil.result(s2[s+83]+s2[s+84],1,"A"));
        //风机卸荷平均电流（A)
        skillstatusEntity.setSsUdfchar21(PublicUtil.result(s2[s+81]+s2[s+82],1,"A"));
        //风机当前转速（Rpm）
        skillstatusEntity.setSsUdfchar22(PublicUtil.result(s2[s+79]+s2[s+80],0,"Rpm"));
        //风机运行状态 有误
        skillstatusEntity.setSsUdfchar23("有误");
        //第一路输出电流（A)
        skillstatusEntity.setSsUdfchar24(PublicUtil.result(s2[s+69]+s2[s+70],1,"A"));
        //第一路输出功率（W)
        skillstatusEntity.setSsUdfchar25(PublicUtil.result(s2[s+73]+s2[s+74],0,"W"));
        //第一路输出状态
        skillstatusEntity.setSsUdfchar26("有误");
        //第一路控制模式
        skillstatusEntity.setSsUdfchar27("有误");
        //控制箱温度
        skillstatusEntity.setSsUdfchar29(PublicUtil.result(s2[s+10]+s2[s+11],2,"A"));
        //摄像头当前状态
        skillstatusEntity.setSsUdfchar32("有误");
        //摄像头连接状态
        skillstatusEntity.setSsUdfchar33("有误");
        //摄像头连接状态
        skillstatusEntity.setSsUdfchar34("有误");
        //摄像头输出电压（V)
        skillstatusEntity.setSsUdfchar35(PublicUtil.result(s2[s+10]+s2[s+11],2,"V"));

        //光伏板总发电量
        skillstatusEntity.setSsUdfnumber01(Double.valueOf(PublicUtil.result(s2[s+51]+s2[s+52]+s2[s+53],1,"")));
        //风机总发电量
        skillstatusEntity.setSsUdfnumber02(Double.valueOf(PublicUtil.result(s2[s+48]+s2[s+49]+s2[s+50],1,"")));
        return skillstatusEntity;
    }

    @Override
    public Map getDlMap(String username) {
        return skillstatusDao.getDlMap(username);
    }

    @Override
    public int selectCountByUser(Map map) {
        return skillstatusDao.selectCountByUser(map);
    }

}
